package com.example.cursor_controller;

import com.example.cursor_service.PermissionService;
import com.example.cursor_orm.model.Permission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/permissions")
@Tag(name = "权限管理", description = "权限相关的API接口")
public class PermissionController {
    private final PermissionService permissionService;
    
    @Autowired
    public PermissionController(PermissionService permissionService) {
        this.permissionService = permissionService;
    }
    
    @PostMapping
    @Operation(summary = "创建权限", description = "创建新权限")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "权限创建成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误")
    })
    public Permission createPermission(@RequestBody Permission permission) { 
        return permissionService.createPermission(permission); 
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取权限", description = "根据权限ID获取权限信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取权限成功"),
        @ApiResponse(responseCode = "404", description = "权限不存在")
    })
    public Permission getPermissionById(@Parameter(description = "权限ID") @PathVariable Integer id) { 
        return permissionService.getPermissionById(id); 
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新权限", description = "根据权限ID更新权限信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "权限更新成功"),
        @ApiResponse(responseCode = "404", description = "权限不存在")
    })
    public Permission updatePermission(@Parameter(description = "权限ID") @PathVariable Integer id, @RequestBody Permission permission) { 
        return permissionService.updatePermission(id, permission); 
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除权限", description = "根据权限ID删除权限")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "权限删除成功"),
        @ApiResponse(responseCode = "404", description = "权限不存在")
    })
    public boolean deletePermission(@Parameter(description = "权限ID") @PathVariable Integer id) { 
        return permissionService.deletePermission(id); 
    }
    
    @GetMapping
    @Operation(summary = "获取权限列表", description = "分页获取权限列表")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取权限列表成功")
    })
    public List<Permission> getPermissionList(@Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int page, 
                                            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") int size) { 
        return permissionService.getPermissionList(page, size); 
    }
    
    @GetMapping("/tree")
    @Operation(summary = "获取权限树", description = "获取权限的树形结构")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取权限树成功")
    })
    public List<Permission> getPermissionTree() { 
        return permissionService.getPermissionTree(); 
    }
    
    @PostMapping("/{id}/roles")
    @Operation(summary = "为角色分配权限", description = "为角色分配指定权限")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "权限分配成功"),
        @ApiResponse(responseCode = "404", description = "权限或角色不存在")
    })
    public boolean assignPermissionToRole(@Parameter(description = "权限ID") @PathVariable Integer id, 
                                        @Parameter(description = "角色ID") @RequestParam Integer roleId) { 
        return permissionService.assignPermissionToRole(id, roleId); 
    }
    
    @DeleteMapping("/{id}/roles/{roleId}")
    @Operation(summary = "移除角色权限", description = "移除角色的指定权限")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "权限移除成功"),
        @ApiResponse(responseCode = "404", description = "权限或角色不存在")
    })
    public boolean removePermissionFromRole(@Parameter(description = "权限ID") @PathVariable Integer id, 
                                          @Parameter(description = "角色ID") @PathVariable Integer roleId) { 
        return permissionService.removePermissionFromRole(id, roleId); 
    }
} 